package com.robinmouren.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.robinmouren.annotation.AutoFill;
import com.robinmouren.constant.AutoFillConstant;
import com.robinmouren.constant.MessageConstant;
import com.robinmouren.context.BaseContext;
import com.robinmouren.dto.*;
import com.robinmouren.entity.*;
import com.robinmouren.enumeration.OperationType;
import com.robinmouren.exception.NullInfoException;
import com.robinmouren.exception.NullUserInfoException;
import com.robinmouren.result.Result;
import com.robinmouren.service.impl.*;
import com.robinmouren.vo.ClientContractPageVo;
import com.robinmouren.vo.ClientInfoVo;
import com.robinmouren.vo.OutletsPageVo;
import com.robinmouren.vo.ToGetDriverQuoteVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Robinmouren
 * @since 2024-05-11
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private DriverServiceImpl driverService;
    @Autowired
    private QuoteServiceImpl quoteService;
    @Autowired
    private OutletsServiceImpl outletsService;
    @Autowired
    private SplitRouteServiceImpl routeService;
    @Autowired
    private SplitGoodsServiceImpl goodsService;
    @Autowired
    private ClientContractServiceImpl clientService;

    /**
     * 客户端创建询价单
     *
     * @return
     */
    @PostMapping("/quotation/create")
    public Result createQuotation(@RequestBody QuotationDto quotationDto) {
        log.info("客户创建询价单，信息：{}", quotationDto);

        orderService.quotationCreate(quotationDto);
        return Result.success();
    }


    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/info/get")
    public Result<User> getUserInfo(String id) {
        log.info("获取用户信息，用户id：{}", id);

        User user = userService.userInfoGet(id);
        return Result.success(user);
    }

    /**
     * 根据订单号获取客户信息
     *
     * @return
     */
    @GetMapping("/getByNumber")
    public Result<ClientInfoVo> clientGetByNumber(String orderNumber) {
        log.info("根据订单号获取客户信息，订单号：{}", orderNumber);

        ClientInfoVo client = userService.clientGetByNumber(orderNumber);
        return Result.success(client);
    }


    /**
     * 向司机询价
     *
     * @return
     */
    @PostMapping("/quote/toDriver")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.CARRIERS_QUOTE_TO_DRIVER)
    public Result quoteToDriver(@RequestBody QuoteToDriverDto quoteToDriverDto) {
        log.info("向司机报价，信息：{}", quoteToDriverDto);

        driverService.addQuote(quoteToDriverDto);
        return Result.success();
    }


    /**
     * 承运商获取运输询价
     *
     * @return
     */
    @GetMapping("/get/driverQuote")
    public Result<ToGetDriverQuoteVo> toGetDriverQuote(String orderNumber) {
        log.info("承运商获取运输询价，信息：{}", orderNumber);
        ToGetDriverQuoteVo vo = driverService.toGetDriverQuote(orderNumber);
        return Result.success(vo);
    }


    /**
     * 承运商报价
     *
     * @return
     */
    @PostMapping("/carriers/quote")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.CARRIERS_QUOTE)
    public Result carriersQuote(@RequestBody CarriersQuoteDto dto) {
        log.info("承运商报价，信息：{}", dto);

        driverService.carriersQuote(dto);
        return Result.success();
    }


    /**
     * 根据订单号获取报价
     * @return
     */
    @GetMapping("/carriers/quote/getById")
    public Result<Quote> carriersQuoteGetById(String orderNumber) {
        log.info("根据订单号获取报价，订单号：{}", orderNumber);

        Quote quote = quoteService.quoteGetById(orderNumber);
        return Result.success(quote);
    }

    /**
     * 客户下订单
     * @return
     */
    @PostMapping("/client/order")
    public Result clientOrderCreate(@RequestBody ClientOrderDto dto) {
        log.info("客户下单，信息：{}", dto);

        orderService.clientPlaceOrder(dto);
        return Result.success();
    }


    /**
     * 承运商添加网点
     * @param dto
     * @return
     */
    @PostMapping("/outlets/add")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.ADD_OUTLETS)
    public Result carriersAddOutlets(@RequestBody OutletsDto dto){
        log.info("承运商添加网点，信息：{}", dto);

        outletsService.saveInfo(dto);
        return Result.success();
    }


    /**
     * 网点分页查询
     * @param dto
     * @return
     */
    @PostMapping("/outlets/page")
    public Result<Page<OutletsPageVo>> carriersOutletsPage(@RequestBody QuotationPageDto dto){
        log.info("网点分页查询，信息：{}", dto);

        Page<OutletsPageVo> page = outletsService.outletsPage(dto);
        return Result.success(page);
    }


    /**
     * 网点根据id查询
     * @param id
     * @return
     */
    @GetMapping("/outlets/getById")
    public Result<OutletsPageVo> outletGetById(String id){
        log.info("网点根据id查询，信息：{}", id);

        Outlets outlets = outletsService.getById(id);
        if(outlets == null){
            throw new NullInfoException(MessageConstant.ORDER_IS_NULL);
        }
        OutletsPageVo outletsPageVo = new OutletsPageVo();
        BeanUtils.copyProperties(outlets,outletsPageVo);
        outletsPageVo.setId(outlets.getId().toString());
        return Result.success(outletsPageVo);
    }


    /**
     * 根据id修改网点状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/outlets/update/status")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.UPDATE_OUTLETS_STATUS)
    public Result outletUpdateById(String id,Integer status){
        log.info("网点根据id查询，信息：{}，状态：{}", id,status);

        Outlets outlets = new Outlets();
        outlets.setId(Long.valueOf(id));
        outlets.setStatus(status);
        outletsService.updateById(outlets);
        return Result.success();
    }


    /**
     * 修改网点信息
     * @param dto
     * @return
     */
    @PostMapping("/outlets/update")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.UPDATE_OUTLETS)
    public Result outletUpdate(@RequestBody OutletsDto dto){
        log.info("修改网点信息，信息：{}", dto);

        Outlets outlets = new Outlets();
        BeanUtils.copyProperties(dto,outlets);
        outlets.setId(Long.valueOf(dto.getId()));
        outletsService.updateById(outlets);
        return Result.success();
    }


    /**
     * 保存拆分后的路线信息
     * @param dto
     * @return
     */
    @PostMapping("/outlets/route/add")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.SPLITS_ROUTE)
    public Result splitRouteAdd(@RequestBody SplitRouteDto dto){
        log.info("保存拆分后的路线信息，信息：{}", dto);
        routeService.saveRoute(dto);
        return Result.success();
    }

    /**
     * 保存拆分后的货物信息
     * @param dto
     * @return
     */
    @PostMapping("/outlets/goods/add")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.SPLITS_GOODS)
    public Result splitGoodsAdd(@RequestBody SplitGoodsDto dto){
        log.info("保存拆分后的货物信息，信息：{}", dto);

        goodsService.saveGoods(dto);
        return Result.success();
    }



    /**
     * 根据订单号获取货物拆分信息
     * @param orderNumber
     * @return
     */
    @GetMapping("/outlets/goods/getByNum")
    public Result<List<SplitGoodsList>> splitGoodsGetByNum(String orderNumber){
        log.info("根据订单号获取货物拆分信息，信息：{}", orderNumber);

        List<SplitGoodsList> list = goodsService.getByNum(orderNumber);
        return Result.success(list);
    }



    /**
     * 根据订单号获取路线拆分信息
     * @param orderNumber
     * @return
     */
    @GetMapping("/outlets/route/getByNum")
    public Result<List<SplitRouteList>> splitRouteGetByNum(String orderNumber){
        log.info("根据订单号获取路线拆分信息，信息：{}", orderNumber);

        List<SplitRouteList> list = routeService.getByNum(orderNumber);
        return Result.success(list);
    }

    /**
     * 根据订单号取消所有拆分信息
     * @param orderNumber
     * @return
     */
    @DeleteMapping("/outlets/split/cancel")
    @AutoFill(value = OperationType.RECORDING,context = AutoFillConstant.CANCEL_SPLITS)
    public Result splitRouteCancel(String orderNumber){
        log.info("根据订单号取消所有拆分信息，信息：{}", orderNumber);

        orderService.cancelSplit(orderNumber);
        return Result.success();
    }


    /**
     * 客户合同分页查询
     * @param dto
     * @return
     */
    @PostMapping("/contract/client/page")
    public Result<Page<ClientContractPageVo>> clientContractPage(@RequestBody QuotationPageDto dto){
        log.info("客户合同分页查询，信息：{}", dto);

        Page<ClientContractPageVo> page = clientService.clientPage(dto);
        return Result.success(page);
    }

}
